#include "RibbonPosnState.h"
#include "PlatformTypes.h"
#include "LampManager.h"

RibbonPosnStateMachine g_RibbonPosnStateMachine;

void InitRibbonPosnStateMachine(void)
{
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.Entry = RibbonPosnCtrlStateMachineAgainEntry;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.Excute = RibbonPosnCtrlStateMachineAgainExcute;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.Exit = RibbonPosnCtrlStateMachineAgainExit;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.waitExitCount_ = 0;

    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.Entry = RibbonPosnCtrlStateMachineOffEntry;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.Excute = RibbonPosnCtrlStateMachineOffExcute;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.Exit = RibbonPosnCtrlStateMachineOffExit;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.waitExitCount_ = 0;


    g_RibbonPosnStateMachine.sm_.Entry = RibbonPosnStateEntry;
    g_RibbonPosnStateMachine.sm_.Excute = RibbonPosnStateExcute;
    g_RibbonPosnStateMachine.sm_.Exit = RibbonPosnStateExit;
    g_RibbonPosnStateMachine.sm_.waitExitCount_ = 0;
    g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOn_ = &g_RibbonPosnCtrlStateMachineOn;
    InitRibbonPosnCtrlStateMachineOn();
}

static void RibbonPosnStateExitWithState(RibbonPosnStateMachine* normalStateMachine, RibbonPosnCtrlState inputState)
{
    switch(inputState)
    {
        case RibbonPosnCtrlState_Init:
        case RibbonPosnCtrlState_Off:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.Exit();
            break;
        }

        case RibbonPosnCtrlState_On:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOn_->sm_.Exit();
            break;
        }

        case RibbonPosnCtrlState_Again:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.Exit();
            break;
        }

        default:
        {
            break;
        }
    }
}

static void RibbonPosnStateExcuteWithState(RibbonPosnStateMachine* normalStateMachine, RibbonPosnCtrlState inputState)
{
    switch(inputState)
    {
        case RibbonPosnCtrlState_Init:
        {
            //Ignore
            break;
        }

        case RibbonPosnCtrlState_Off:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.Excute();
            break;
        }

        case RibbonPosnCtrlState_On:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOn_->sm_.Excute();
            break;
        }

        case RibbonPosnCtrlState_Again:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.Excute();
            break;
        }

        default:
        {
            break;
        }

    }
}

static void RibbonPosnStateEntryWithState(RibbonPosnStateMachine* normalStateMachine, RibbonPosnCtrlState inputState)
{
    switch(inputState)
    {
        case RibbonPosnCtrlState_Init:
        {
            //Ignore
            break;
        }

        case RibbonPosnCtrlState_Off:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOff_.sm_.Entry();
            break;
        }

        case RibbonPosnCtrlState_On:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineOn_->sm_.Entry();
            break;
        }

        case RibbonPosnCtrlState_Again:
        {
            g_RibbonPosnStateMachine.ribbonPosnCtrlStateMachineAgain_.sm_.Entry();
            break;
        }

        default:
        {
            break;
        }

    }
}

void RibbonPosnStateEntry(void)
{
    // std::cout << "RibbonPosnStateEntry" << std::endl;
}

void RibbonPosnStateExcute(void)
{
    // std::cout << "RibbonPosnStateExcute" << std::endl;
    if(g_TurnLastState.ribbonPosnCtrlState_ != g_TurnNewState.ribbonPosnCtrlState_)
    {
        RibbonPosnStateExitWithState(&g_RibbonPosnStateMachine, g_TurnLastState.ribbonPosnCtrlState_);
        RibbonPosnStateEntryWithState(&g_RibbonPosnStateMachine, g_TurnNewState.ribbonPosnCtrlState_);
    }
    RibbonPosnStateExcuteWithState(&g_RibbonPosnStateMachine, g_TurnNewState.ribbonPosnCtrlState_);

    UpdateRibbonPosnCtrlState(g_TurnNewState.ribbonPosnCtrlState_, &g_TurnLastState.ribbonPosnCtrlState_);
}

void RibbonPosnStateExit(void)
{
    // std::cout << "RibbonPosnStateExit" << std::endl;
   RibbonPosnStateExitWithState(&g_RibbonPosnStateMachine, g_TurnNewState.ribbonPosnCtrlState_);
   
    g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_OFF;
    g_RibbonView.model_->frntRibbonLampEffectSt_     = RibbonEffectSel_Initial;

   UpdateRibbonPosnCtrlState(RibbonPosnCtrlState_Invaild, &g_TurnLastState.ribbonPosnCtrlState_);
}


